package com.goldcn.enumeration;

import com.goldcn.common.HttpUtil;
import com.goldcn.common.exception.ServiceException;
import com.goldcn.common.model.DataResult;
import com.goldcn.config.ProConstants;
import com.google.common.base.Objects;
import org.apache.commons.collections.MapUtils;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * Created by Administrator on 2016/10/18.
 */
public class TopDeptEnum {

    private Integer seq;
    private Long deptID;//总部
    private String deptName;
    private Long companyID;

    TopDeptEnum(Long deptID,String deptName,Long companyID) {
        this.deptID = deptID;
        this.deptName = deptName;
        this.companyID = companyID;
    }

    public Integer getSeq() {
        return seq;
    }

    public void setSeq(Integer seq) {
        this.seq = seq;
    }

    public Long getDeptID() {
        return deptID;
    }

    public void setDeptID(Long deptID) {
        this.deptID = deptID;
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    public Long getCompanyID() {
        return companyID;
    }

    public void setCompanyID(Long companyID) {
        this.companyID = companyID;
    }

    public static TopDeptEnum valuekOf(Long companyID, RestTemplate restTemplate) {
        DataResult result = HttpUtil.httpGet(ProConstants.getTopOrganizational+companyID,restTemplate);
        if(result == null || !Objects.equal(1,result.getIsSuccess())){
           throw new ServiceException("未获取到总部数据");
        }
        Map<String,Object> organizational = (Map<String, Object>) result.getData();
        TopDeptEnum topDeptEnum = new TopDeptEnum(MapUtils.getLong(organizational,"oID"),
                (MapUtils.getString(organizational,"organizationalName")),companyID);
        return topDeptEnum;
    }
}
